{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "512e3683",
   "metadata": {},
   "source": [
    "## 1. API 币本位合约交易账户接口总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbf7660a",
   "metadata": {},
   "source": [
    "|方法|解释|Path|\n",
    "|:---|:---|:---|\n",
    "|set_positionSide_dual|更改持仓模式|/dapi/v1/positionSide/dual|\n",
    "|get_positionSide_dual|查询持仓模式|/dapi/v1/positionSide/dual|\n",
    "|set_order|下单|/dapi/v1/order|\n",
    "|set_order_test|测试下单接口 (TRADE)|/dapi/v1/order/test|\n",
    "|alter_order|修改订单|/dapi/v1/order|\n",
    "|set_batchOrders|批量下单|/dapi/v1/batchOrders|\n",
    "|alter_batchOrders|批量修改订单|/dapi/v1/batchOrders|\n",
    "|get_orderAmendment|查询订单修改历史|/dapi/v1/orderAmendment|\n",
    "|get_order|查询订单|/dapi/v1/order|\n",
    "|cancel_order|撤销订单|/dapi/v1/order|\n",
    "|cancel_allOpenOrders|撤销全部订单|/dapi/v1/allOpenOrders|\n",
    "|cancel_batchOrders|批量撤销订单|/dapi/v1/batchOrders|\n",
    "|set_countdownCancelAll|倒计时撤销所有订单|/dapi/v1/countdownCancelAll|\n",
    "|get_openOrder|查询当前挂单|/dapi/v1/openOrder|\n",
    "|get_openOrders|查看当前全部挂单|/dapi/v1/openOrders|\n",
    "|get_allOrders|查询所有订单(包括历史订单)|/dapi/v1/allOrders|\n",
    "|get_balance|账户余额|/dapi/v1/balance|\n",
    "|get_account|账户信息|/dapi/v1/account|\n",
    "|set_leverage|调整开仓杠杆|/dapi/v1/leverage|\n",
    "|set_marginType|变换逐全仓模式|/dapi/v1/marginType|\n",
    "|set_positionMargin|调整逐仓保证金|/dapi/v1/positionMargin|\n",
    "|get_positionMargin_history|逐仓保证金变动历史|/dapi/v1/positionMargin/history|\n",
    "|get_positionRisk|用户持仓风险|/dapi/v1/positionRisk|\n",
    "|get_userTrades|账户成交历史|/dapi/v1/userTrades|\n",
    "|get_income|获取账户损益资金流水|/dapi/v1/income|\n",
    "|get_leverageBracket_by_pair|标的交易对默认杠杆分层标准 (USER_DATA)|/dapi/v2/leverageBracket|\n",
    "|get_leverageBracket_by_symbol|交易对杠杆分层标准 (USER_DATA)|/dapi/v2/leverageBracket|\n",
    "|get_forceOrders|用户强平单历史|/dapi/v1/forceOrders|\n",
    "|get_adlQuantile|持仓ADL队列估算|/dapi/v1/adlQuantile|\n",
    "|get_commissionRate|用户手续费率|/dapi/v1/commissionRate|\n",
    "|get_income_asyn|获取合约资金流水下载Id|/dapi/v1/income/asyn|\n",
    "|get_income_asyn_id|通过下载Id获取合约资金流水下载链接|/dapi/v1/income/asyn/id|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fc6c7ab",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "35aaa2b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.api import CM\n",
    "from binance_interface.app.utils import eprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a9bf9989",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "cm = CM(\n",
    "    key=key, secret=secret,\n",
    "    proxy_host=proxy_host\n",
    ")\n",
    "accountTrade = cm.accountTrade"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a362d7d7",
   "metadata": {},
   "source": [
    "## 3. 账户相关"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a66b56d7",
   "metadata": {},
   "source": [
    "### 3.1 账户余额V2 get_balance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e13719b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'accountAlias': 'FzTiAusRuXmYTisR',\n",
      "           'asset': 'ATOM',\n",
      "           'balance': '0.00000000',\n",
      "           'withdrawAvailable': '0.00000000',\n",
      "           'updateTime': 0,\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'FzTiAusRuXmYTisR',\n",
      "           'asset': 'LINK',\n",
      "           'balance': '0.00000000',\n",
      "           'withdrawAvailable': '0.00000000',\n",
      "           'updateTime': 0,\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'FzTiAusRuXmYTisR',\n",
      "           'asset': 'XRP',\n",
      "           'balance': '0.00000000',\n",
      "           'withdrawAvailable': '0.00000000',\n",
      "           'updateTime': 0,\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'FzTiAusRuXmYTisR',\n",
      "           'asset': 'LTC',\n",
      "           'balance': '0.00000000',\n",
      "           'withdrawAvailable': '0.00000000',\n",
      "           'updateTime': 0,\n",
      "           '...': '......'},\n",
      "          {'accountAlias': 'FzTiAusRuXmYTisR',\n",
      "           'asset': 'ROSE',\n",
      "           'balance': '0.00000000',\n",
      "           'withdrawAvailable': '0.00000000',\n",
      "           'updateTime': 0,\n",
      "           '...': '......'},\n",
      "          '......'],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "balance_result = accountTrade.get_balance()\n",
    "eprint(balance_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22fad839",
   "metadata": {},
   "source": [
    "### 3.2 账户信息V2 get_account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "64605f95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'feeTier': 0,\n",
      "          'canTrade': True,\n",
      "          'canDeposit': True,\n",
      "          'canWithdraw': True,\n",
      "          'updateTime': 0,\n",
      "          'assets': [{'asset': 'APE',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     {'asset': 'FTM',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     {'asset': 'TRX',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     {'asset': 'NEAR',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     {'asset': 'LUNA',\n",
      "                      'walletBalance': '0.00000000',\n",
      "                      'unrealizedProfit': '0.00000000',\n",
      "                      'marginBalance': '0.00000000',\n",
      "                      'maintMargin': '0.00000000',\n",
      "                      '...': '......'},\n",
      "                     '......'],\n",
      "          'positions': [{'symbol': 'ALGOUSD_PERP',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        {'symbol': 'ALGOUSD_PERP',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        {'symbol': 'ALGOUSD_PERP',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        {'symbol': 'ENSUSD_PERP',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        {'symbol': 'ENSUSD_PERP',\n",
      "                         'initialMargin': '0',\n",
      "                         'maintMargin': '0',\n",
      "                         'unrealizedProfit': '0.00000000',\n",
      "                         'positionInitialMargin': '0',\n",
      "                         '...': '......'},\n",
      "                        '......']},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "account_result = accountTrade.get_account()\n",
    "eprint(account_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f76a19fd",
   "metadata": {},
   "source": [
    "### 3.3 用户手续费率 get_commissionRate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "365ff9ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'ETHUSD_PERP', 'makerCommissionRate': '0.000200', 'takerCommissionRate': '0.000500'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "commissionRate_result = accountTrade.get_commissionRate(symbol='ETHUSD_PERP')\n",
    "eprint(commissionRate_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddfd4478",
   "metadata": {},
   "source": [
    "### 3.4 账户成交历史 get_userTrades"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7985b98d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'ETHUSD_PERP',\n",
      "           'id': 685068357,\n",
      "           'orderId': 67341315779,\n",
      "           'pair': 'ETHUSD',\n",
      "           'side': 'SELL',\n",
      "           'price': '2291.66',\n",
      "           'qty': '1',\n",
      "           'realizedPnl': '0',\n",
      "           'marginAsset': 'ETH',\n",
      "           'baseQty': '0.00436365',\n",
      "           'commission': '0.00000218',\n",
      "           'commissionAsset': 'ETH',\n",
      "           'time': 1703344228754,\n",
      "           'positionSide': 'SHORT',\n",
      "           'buyer': False,\n",
      "           'maker': False},\n",
      "          {'symbol': 'ETHUSD_PERP',\n",
      "           'id': 685068387,\n",
      "           'orderId': 67341329490,\n",
      "           'pair': 'ETHUSD',\n",
      "           'side': 'BUY',\n",
      "           'price': '2291.32',\n",
      "           'qty': '1',\n",
      "           'realizedPnl': '0',\n",
      "           'marginAsset': 'ETH',\n",
      "           'baseQty': '0.00436430',\n",
      "           'commission': '0.00000218',\n",
      "           'commissionAsset': 'ETH',\n",
      "           'time': 1703344262863,\n",
      "           'positionSide': 'LONG',\n",
      "           'buyer': True,\n",
      "           'maker': False},\n",
      "          '......'],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "userTrade_result = accountTrade.get_userTrades(symbol='ETHUSD_PERP')\n",
    "eprint(userTrade_result, data_length=2, length=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e12255fd",
   "metadata": {},
   "source": [
    "## 4. 持仓与杠杆"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20052ef1",
   "metadata": {},
   "source": [
    "### 4.1 更改持仓模式 set_positionSide_dual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ed7f2529",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': -4059, 'msg': 'No need to change position side.'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# \"true\": 双向持仓模式；\"false\": 单向持仓模式\n",
    "# 如果已经是该持仓模式，返回：{'code': -4059, 'msg': 'No need to change position side.'}\n",
    "accountTrade.set_positionSide_dual(dualSidePosition=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a258cedb",
   "metadata": {},
   "source": [
    "### 4.2 查询持仓模式 get_positionSide_dual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9e7d4651",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': 200, 'data': {'dualSidePosition': True}, 'msg': ''}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accountTrade.get_positionSide_dual() "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff948a6a",
   "metadata": {},
   "source": [
    "### 4.3 变换逐全仓模式 set_marginType"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bdcfcfa7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': -4046, 'msg': 'No need to change margin type.'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 保证金模式 ISOLATED(逐仓), CROSSED(全仓)\n",
    "# 如果已经是该模式，返回{'code': -4046, 'msg': 'No need to change margin type.'}\n",
    "accountTrade.set_marginType(symbol='ETHUSD_PERP', marginType='ISOLATED')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb44c41e",
   "metadata": {},
   "source": [
    "### 4.4 调整开仓杠杆 set_leverage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "720ca71f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': 200,\n",
       " 'data': {'symbol': 'ETHUSD_PERP', 'leverage': 1, 'maxQty': '50000'},\n",
       " 'msg': ''}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accountTrade.set_leverage(symbol='ETHUSD_PERP', leverage=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37ddd8c0",
   "metadata": {},
   "source": [
    "## 5. 交易与订单"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "875fb920",
   "metadata": {},
   "source": [
    "### 5.1 下单 set_order"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "361e8cb4",
   "metadata": {},
   "source": [
    "#### 5.1.1 限单价开仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "95ac0da2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 70610442187,\n",
      "          'symbol': 'ETHUSD_PERP',\n",
      "          'pair': 'ETHUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'CLLRMwe23TQ8P5wukVP2Hl',\n",
      "          'price': '2000',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'updateTime': 1706246830608},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 开多\n",
    "set_order_result = accountTrade.set_order(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    price='2000',\n",
    "    quantity='1',\n",
    "    side='BUY',\n",
    "    type='LIMIT',\n",
    "    timeInForce='GTC',\n",
    "    positionSide='LONG',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b895dc8f",
   "metadata": {},
   "source": [
    "#### 5.1.2 市单价开仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "211f7ced",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 70610458314,\n",
      "          'symbol': 'ETHUSD_PERP',\n",
      "          'pair': 'ETHUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'qajOGO6Z7jpWW7xpccTKlr',\n",
      "          'price': '0',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'MARKET',\n",
      "          'reduceOnly': False,\n",
      "          'closePosition': False,\n",
      "          'side': 'SELL',\n",
      "          'positionSide': 'SHORT',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'MARKET',\n",
      "          'updateTime': 1706246838640},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 开空\n",
    "set_order_result = accountTrade.set_order(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    quantity='1',\n",
    "    side='SELL',\n",
    "    type='MARKET',\n",
    "    positionSide='SHORT',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52a367a0",
   "metadata": {},
   "source": [
    "#### 5.1.3 限单价平仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "aceaebc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 70610674285,\n",
      "          'symbol': 'ETHUSD_PERP',\n",
      "          'pair': 'ETHUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'iJr8aKn4ihHghIrUxM6HGz',\n",
      "          'price': '2500',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': True,\n",
      "          'closePosition': False,\n",
      "          'side': 'SELL',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'updateTime': 1706247026725},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 平多\n",
    "set_order_result = accountTrade.set_order(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    price='2500',\n",
    "    quantity='1',\n",
    "    side='SELL',\n",
    "    type='LIMIT',\n",
    "    timeInForce='GTC',\n",
    "    positionSide='LONG',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b600fe72",
   "metadata": {},
   "source": [
    "#### 5.1.4 市单价平仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "550c6336",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 70610682867,\n",
      "          'symbol': 'ETHUSD_PERP',\n",
      "          'pair': 'ETHUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'A7Ac9oiN6Uis0vc4HBrc6p',\n",
      "          'price': '0',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'MARKET',\n",
      "          'reduceOnly': True,\n",
      "          'closePosition': False,\n",
      "          'side': 'BUY',\n",
      "          'positionSide': 'SHORT',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'MARKET',\n",
      "          'updateTime': 1706247044607},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 平空\n",
    "set_order_result = accountTrade.set_order(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    quantity='1',\n",
    "    side='BUY',\n",
    "    type='MARKET',\n",
    "    positionSide='SHORT',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4efababa",
   "metadata": {},
   "source": [
    "### 5.2 当前挂单 get_openOrders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a5226be4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 70610674285,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'iJr8aKn4ihHghIrUxM6HGz',\n",
      "           '...': '......'},\n",
      "          {'orderId': 70610442187,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'CLLRMwe23TQ8P5wukVP2Hl',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "openOrderList_result = accountTrade.get_openOrders(symbol='ETHUSD_PERP')\n",
    "eprint(openOrderList_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e0d3a70",
   "metadata": {},
   "source": [
    "### 5.3 查询订单 get_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5a0d4923",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 70610674285,\n",
      "          'symbol': 'ETHUSD_PERP',\n",
      "          'pair': 'ETHUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'iJr8aKn4ihHghIrUxM6HGz',\n",
      "          'price': '2500',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': True,\n",
      "          'closePosition': False,\n",
      "          'side': 'SELL',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'time': 1706247026725,\n",
      "          'updateTime': 1706247026725},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "order_result = accountTrade.get_order(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    orderId='70610674285',\n",
    ")\n",
    "eprint(order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6aca6432",
   "metadata": {},
   "source": [
    "### 5.4 修改订单 alter_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e57dd79b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 70610674285,\n",
      "          'symbol': 'ETHUSD_PERP',\n",
      "          'pair': 'ETHUSD',\n",
      "          'status': 'NEW',\n",
      "          'clientOrderId': 'iJr8aKn4ihHghIrUxM6HGz',\n",
      "          'price': '2600',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': True,\n",
      "          'closePosition': False,\n",
      "          'side': 'SELL',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'updateTime': 1706247087696},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "alter_order_result = accountTrade.alter_order(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    orderId='70610674285',\n",
    "    price='2600',\n",
    "    side='SELL',\n",
    "    quantity='1',\n",
    ")\n",
    "eprint(alter_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90a6982e",
   "metadata": {},
   "source": [
    "### 5.5 批量下单 set_batchOrders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a8dfd8db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'orderId': 70610709872,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'vNxmIN08X2UakcOkI2OrfG0',\n",
      "           '...': '......'},\n",
      "          {'orderId': 70610709873,\n",
      "           'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'status': 'NEW',\n",
      "           'clientOrderId': 'N02rKwVjOc5SlRtWWVt5eK1',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_batchOrders_result = accountTrade.set_batchOrders(\n",
    "    batchOrders=[\n",
    "        dict(symbol='ETHUSD_PERP', price='1500', quantity='1', side='BUY', type='LIMIT', timeInForce='GTC',\n",
    "             positionSide='LONG', ),\n",
    "        dict(symbol='ETHUSD_PERP', price='1600', quantity='1', side='BUY', type='LIMIT', timeInForce='GTC',\n",
    "             positionSide='LONG', ),\n",
    "    ]\n",
    ")\n",
    "eprint(set_batchOrders_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94d78299",
   "metadata": {},
   "source": [
    "### 5.6 撤销订单 cancel_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5ac84767",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'orderId': 70610674285,\n",
      "          'symbol': 'ETHUSD_PERP',\n",
      "          'pair': 'ETHUSD',\n",
      "          'status': 'CANCELED',\n",
      "          'clientOrderId': 'iJr8aKn4ihHghIrUxM6HGz',\n",
      "          'price': '2600',\n",
      "          'avgPrice': '0.00',\n",
      "          'origQty': '1',\n",
      "          'executedQty': '0',\n",
      "          'cumQty': '0',\n",
      "          'cumBase': '0',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'reduceOnly': True,\n",
      "          'closePosition': False,\n",
      "          'side': 'SELL',\n",
      "          'positionSide': 'LONG',\n",
      "          'stopPrice': '0',\n",
      "          'workingType': 'CONTRACT_PRICE',\n",
      "          'priceProtect': False,\n",
      "          'origType': 'LIMIT',\n",
      "          'updateTime': 1706247105009},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "cancel_order_result = accountTrade.cancel_order(\n",
    "    symbol='ETHUSD_PERP',\n",
    "    orderId='70610674285'\n",
    ")\n",
    "eprint(cancel_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5abfea09",
   "metadata": {},
   "source": [
    "### 5.7 撤销单一交易对的所有挂单 cancel_allOpenOrders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c8317589",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'msg': 'The operation of cancel all open order is done.'}\n"
     ]
    }
   ],
   "source": [
    "cancel_openOrders_result = accountTrade.cancel_allOpenOrders(symbol='ETHUSD_PERP')\n",
    "eprint(cancel_openOrders_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
